﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace Hired
{
    public partial class viewempprofile : System.Web.UI.Page
    {
        private CompanyAccount ca;
        public string OnClick;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request.QueryString["username"] != null)
            {
                ca = DBCompanyAccount.RetrieveCompanyAccountByUsername((string)Request.QueryString["username"], true);
                if (ca != null)
                {
                    lblUsername.Text = ca.Username;
                    lblBuddyUsername.Text = ca.Username;
                    lblRecipient.Text = ca.Username;
                    edsEmp.WhereParameters[0].DefaultValue = ca.CAccountID;
                }
                else
                {
                    Response.Redirect("home.aspx");
                }
            }
            else
            {
                Response.Redirect("home.aspx");
            }

            //Wall Thread
            List<Thread> wallThread = DBThread.RetrieveThreadsByUserID(ca.CAccountID);

            if (wallThread.Count != 0)
            {
                foreach (Thread t in wallThread)
                {
                    #region Thread
                    UCComments tc = (UCComments)LoadControl("UCComments.ascx");
                    pnlContainer.Controls.Add(tc);

                    // accessing ThreadComment.ascx controls
                    Image threadImgProfilePic = (Image)tc.FindControl("imgProfilePic");
                    Label threadLblCommentContent = (Label)tc.FindControl("lblCommentContent");
                    LinkButton threadBtnUsername = (LinkButton)tc.FindControl("btnUsername");

                    // assigning values to them
                    if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                    {
                        JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                        threadBtnUsername.Text = jsPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                    }
                    else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                    {
                        CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                        threadBtnUsername.Text = coPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                    }
                    threadLblCommentContent.Text = t.Contents;

                    // assigning values to them
                    foreach (Comment c in t.Comments)
                    {
                        UCComments comment = (UCComments)Page.LoadControl("UCComments.ascx");
                        pnlContainer.Controls.Add(comment);

                        // accessing ThreadUC.ascx controls
                        Panel commentPnlComment = (Panel)comment.FindControl("pnlComment");
                        Image commentImgProfilePic = (Image)comment.FindControl("imgProfilePic");
                        Label commentLblCommentContent = (Label)comment.FindControl("lblCommentContent");
                        LinkButton commentBtnUsername = (LinkButton)comment.FindControl("btnUsername");

                        // assigning values to them
                        commentPnlComment.CssClass = "pnlComment";
                        if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                        {
                            JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                            commentImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                            commentBtnUsername.Text = jsPoster.Username;
                            commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                        }
                        else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                        {
                            CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                            commentImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                            commentBtnUsername.Text = coPoster.Username;
                            commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                        }
                        commentLblCommentContent.Text = t.Contents;
                    }
                    threadLblCommentContent.Text = t.Contents;
                    #endregion

                    #region Comments
                    if (t.Comments.Count != 0)
                    {
                        foreach (Comment c in t.Comments)
                        {
                            UCComments comment = (UCComments)Page.LoadControl("UCComments.ascx");
                            pnlContainer.Controls.Add(comment);

                            // accessing ThreadUC.ascx controls
                            Panel commentPnlComment = (Panel)comment.FindControl("pnlComment");
                            Image commentImgProfilePic = (Image)comment.FindControl("imgProfilePic");
                            Label commentLblCommentContent = (Label)comment.FindControl("lblCommentContent");
                            LinkButton commentBtnUsername = (LinkButton)comment.FindControl("btnUsername");

                            // assigning values to them
                            commentPnlComment.CssClass = "pnlComment";
                            if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                            {
                                JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                                commentBtnUsername.Text = jsPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                            }
                            if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                            {
                                CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                                commentBtnUsername.Text = coPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                            }
                            commentLblCommentContent.Text = c.Contents;
                        }
                    }
                    #endregion

                    UCPostComment pc = (UCPostComment)Page.LoadControl("UCPostComment.ascx");
                    ((Label)pc.FindControl("lblThreadID")).Text = t.ThreadID;
                    pnlContainer.Controls.Add(pc);

                    Button btnPost = (Button)pc.FindControl("btnPost");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void linkBtnViewAll_Click(object sender, EventArgs e)
        {
            Response.Redirect("empViewJob.aspx?company=" + Server.UrlEncode(ca.CAccountID));
        }

        protected void btnWall_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlWall.Visible = true;
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            Thread t = new Thread();
            t.Contents = txtComment.Text;
            t.PosterID = User.Identity.Name;
            if (User.IsInRole("company"))
            {
                t.WallID = ca.CAccountID;
            }
            else if (User.IsInRole("employer"))
            {
                ca = DBCompanyAccount.RetrieveCompanyAccountByEmployerID(User.Identity.Name, false);
                t.WallID = ca.CAccountID;
            }
            DBThread.AddThread(t);
        }

        protected void btnInformation_Click(object sender, ImageClickEventArgs e)
        {

            hidePanels();
            pnlPersonalInformation.Visible = true;
            pnlBuddyList.Visible = false;
            pnlImages.Visible = false;
            pnlWall.Visible = false;

            #region
            //CompanyInfo
            lblCompanyNameValue.Text = ca.Company.CompanyName;
            lblDateFoundedValue.Text = ca.Company.DateFounded.ToString();
            lblContactNumberValue.Text = ca.Company.ContactNumber;
            lblWebsiteValue.Text = ca.Company.WebsiteURL;
            lblEmailAddressValue.Text = ca.Email;
            lblAddressValue.Text = ca.Company.Address;
            lblPostalCodeValue.Text = ca.Company.PostalCode;

            #endregion
        }

        protected void btnBuddyList_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlBuddyList.Visible = true;
            pnlImages.Visible = false;
            pnlPersonalInformation.Visible = false;
            pnlWall.Visible = false;
        }

        protected void btnImages_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlImages.Visible = true;
        }

        protected void btnMessage_Click(object sender, ImageClickEventArgs e)
        {
            // Session["recipient"] = ca.Username;
        }

        //hidePanels()
        protected void hidePanels()
        {
            pnlWall.Visible = false;
            pnlPersonalInformation.Visible = false;
            pnlBuddyList.Visible = false;
            pnlImages.Visible = false;
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            Message m = new Message();
            m.RecipientID = ca.CAccountID;
            m.SenderID = User.Identity.Name;
            m.Title = txtSubject.Text;
            m.Contents = txtContent.Text;
            m.Timestamp = DateTime.Now;

            DBMessage.AddMessage(m);
        }

        protected void btnAddBuddy_Click(object sender, ImageClickEventArgs e)
        {
            BuddyRequestRoster br = new BuddyRequestRoster();
            br.SenderID = User.Identity.Name;
            br.RecipientID = ca.CAccountID;

            if (DBBuddyRequestRoster.addBuddyRequest(br))
            {
                lblConfirmBuddyUsername.Text = "A buddy request have been sent to " + ca.Username + ".";
            }
            else
            {
                lblConfirmBuddyUsername.Text = "Failed to add" + ca.Username + "as buddy. Have you already added him?";
            }

            ModalPopupBuddyConfirm.Show();
        }
    }
}
